import React, {useMemo, useRef} from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';

const LineChartsTrend = ({
                                trendData = {},
                                exchange = '',
                                selectedData
                            }) => {
    const chartContainerRef = useRef(null); // 用于全屏切换的容器引用
    const recentDaysList = Array.isArray(trendData) ? trendData.map(item => item.time_slot) : [];
    // 配置数据 当天没有数据的值设为0
    const formatData = useMemo(() => {
        let newData = {};

        recentDaysList.forEach(() => {
            if (trendData && Object.keys(trendData).length === 0) {
                return {};
            }
            newData.avg_price = trendData.map(item => parseFloat((item.average_price)));
            newData.total_amount = trendData.map(item => parseFloat(item.total_amount));
        });
        return newData;
    }, [trendData]);
    console.log(formatData.avg_price);
    const series = [
        selectedData.includes('avg_price') && {
            name: '平均成交价',
            data: formatData.avg_price,
            yAxis: 0 // 关联到第一个 Y 轴
        },
        selectedData.includes('total_amount') && {
            name: '成交量',
            data: formatData.total_amount,
            yAxis: 1 // 关联到第一个 Y 轴
        },
    ].filter(Boolean); // 去除空值

    // 图表配置
    const options = {
        chart: {
            chart: {
                type: 'line'
            },
            style: {
                fontSize: '1em'
            },
        },
        title: {
            text: exchange,
            align: 'left'
        },
        yAxis: [{ // 第一个 Y 轴
            title: {
                text: '平均成交价'
            }
        }, { // 第二个 Y 轴
            title: {
                text: '成交量'
            },
            opposite: true // 使第二个 Y 轴在右侧
        }],
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle'
        },
        xAxis: {
            categories: recentDaysList.map(item => {
                    return item.substring(11,16)
            })
        },
        series: series,
        tooltip: {
            headerFormat: '<b>{point.x}</b><br>', // 显示时间（X轴）
            pointFormat: '{series.name}: {point.y}', // 显示每个数据点的值
            shared: true, // 显示多个系列的数据
            crosshairs: true, // 显示十字准线
        },
        responsive: {
            rules: [{
                condition: {
                    maxWidth: 500
                },
                chartOptions: {
                    legend: {
                        layout: 'horizontal',
                        align: 'center',
                        verticalAlign: 'bottom'
                    }
                }
            }]
        },
        credits: {
            enabled: false
        }
    };

    return (
        <div ref={chartContainerRef} style={{ cursor: 'pointer' }}>
            <HighchartsReact
                highcharts={Highcharts}
                options={options}
            />
        </div>
    );
};

export default LineChartsTrend;
